package com.yanggu.payment.config;

import com.alipay.api.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @version V1.0
 * @author: YangGu
 * @date: 2022/5/10 15:24
 * @description:
 */
@Data
@Slf4j
@Configuration
@PropertySource("classpath:alipay-sandbox.properties") //读取配置文件
@ConfigurationProperties(prefix = "alipay") //读取alipay节点
public class ALiPayConfig {

    private String appId;

    private String sellerId;

    private String gatewayUrl;

    private String merchantPrivateKey;

    private String alipayPublicKey;

    private String contentKey;

    private String returnUrl;

    private String notifyUrl;

    @Bean
    public AlipayClient alipayClient() throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        //设置网关地址
        alipayConfig.setServerUrl(gatewayUrl);
        //设置应用Id
        alipayConfig.setAppId(appId);
        //设置应用私钥
        alipayConfig.setPrivateKey(merchantPrivateKey);
        //设置请求格式，固定值json
        alipayConfig.setFormat(AlipayConstants.FORMAT_JSON);
        //设置字符集
        alipayConfig.setCharset(AlipayConstants.CHARSET_UTF8);
        //设置支付宝公钥
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        //设置签名类型
        alipayConfig.setSignType(AlipayConstants.SIGN_TYPE_RSA2);
        //构造client
        log.info("初始化AlipayClient对象: {}", alipayConfig);
        return new DefaultAlipayClient(alipayConfig);
    }

}
